package com.example.mybatis.service.impl;

import com.example.mybatis.entity.User;
import com.example.mybatis.mapper.UserMapper;
import com.example.mybatis.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author ：xiongwei
 * @date ：Created in 2020/11/8 21:15
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public int insertUser(User user) throws Exception {
        Assert.notNull(user, "插入的对象不能为null");
        Assert.notNull(user.getUserId(), "userId不能为null");
        Assert.notNull(user.getName(), "名字不能为null");
        log.info("user = {}", user.toString());
        return userMapper.insertUser(user);
    }


    @Override
    public int updateUser(User user) throws Exception {
        Assert.notNull(user, "插入的对象不能为null");
        Assert.notNull(user.getUserId(), "userId不能为null");
        return userMapper.updateUser(user);
    }

    @Override
    public int updateUserBySql(User user) throws Exception {
        if (null == user) {
            throw new Exception("插入的值不能为null");
        }
        if (null == user.getUserId()) {
            throw new Exception("插入的userId不能为null");
        }
        return userMapper.updateUserBySql(user);
    }

    @Override
    public int deleteUser(Long userId) throws Exception {
        if (null == userId) {
            throw new Exception("插入的userId不能为null");
        }
        return userMapper.deleteUser(userId);
    }

    @Override
    public List<User> getUsers() throws Exception {
        return userMapper.findUser();
    }



}
